<template>
  <div>
    <div class="bg-white h-auto my-2 mb-2 py-2">
      <BasicForm @register="registerForm" @submit="handleSubmit" />
      <Description
        size="middle"
        title="漏氢率统计结果"
        :bordered="false"
        :column="2"
        :data="stsResultData"
        :schema="stsResultSchema"
      />
      <a-divider />
      <Description
        size="middle"
        title="计算引用数值"
        :bordered="false"
        :column="4"
        :data="stsVariableData"
        :schema="stsVariableSchema"
      />
      <a-divider />
      <Description
        size="middle"
        title="静态值"
        :bordered="false"
        :column="3"
        :data="stsConstData"
        :schema="stsConstSchema"
      />
    </div>
  </div>
</template>
<script lang="ts">
  import { defineComponent, ref, reactive, onBeforeMount, onMounted, onUnmounted } from 'vue';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { Description } from '/@/components/Description/index';
  import { Divider } from 'ant-design-vue';
  import { useRouter } from 'vue-router';
  import { EditOutlined, EyeOutlined } from '@ant-design/icons-vue';
  import { numberFormatToString } from '/@/utils/numberUtil';

  import {
    HydrogenLeakageStatisticsConditionDto,
    HydrogenLeakageDynamoStatisticsResultDto,
  } from '/@/api/demo/model/yzhModel';
  import { statisticsHydrogenLeakage } from '/@/api/demo/yzh';
  import {
    hydrogenLeakageStatisticsConditionFormSchema,
    stsResultData,
    stsResultSchema,
    stsVariableData,
    stsVariableSchema,
    stsConstData,
    stsConstSchema,
  } from './hydrogen.data';

  import { useMessage } from '/@/hooks/web/useMessage';
  const { createMessage } = useMessage();

  import { Tag } from 'ant-design-vue';
  import { Tooltip } from 'ant-design-vue';

  export default defineComponent({
    name: 'HydrogenLeakageStatisticsIndex',
    components: {
      BasicForm,
      Tag,
      Tooltip,
      Description,
      [Divider.name]: Divider,
    },
    //要再setup函数中获取或使用props，需要在setup函数中获取props对象
    setup() {
      const router = useRouter();

      const stsResultData = reactive({
        dynamoName: '##机组',
        tmrange: '-',
        success: '-',
        msg: '-',
        rate: '-',
        hours: '-',
      });

      const stsVariableData = reactive({
        beginPressureTime: '-',
        beginPressureValue: '-',
        beginAvgTemp: '-',
        beginTemps: '-',
        endPressureTime: '-',
        endPressureValue: '-',
        endAvgTemp: '-',
        endTemps: '-',
      });

      const stsConstData = reactive({
        capacity: '72',
        airPressure: '0.0825',
      });

      const [registerForm, { setFieldsValue, updateSchema, submit }] = useForm({
        labelWidth: 80,
        schemas: hydrogenLeakageStatisticsConditionFormSchema,
        // actionColOptions: {
        //   span: 24,
        // },
      });

      /**
       * 表单提交按钮事件
       */
      function handleSubmit(values: any) {
        console.log(values);

        //先对统计结果置空
        stsResultData.dynamoName = '##机组';
        stsResultData.tmrange = '-';
        stsResultData.success = '统计失败。';
        stsResultData.msg = '-';
        stsResultData.rate = '-';
        stsResultData.hours = '-';
        stsVariableData.beginPressureTime = '-';
        stsVariableData.beginPressureValue = '-';
        stsVariableData.beginAvgTemp = '-';
        stsVariableData.beginTemps = '-';
        stsVariableData.endPressureTime = '-';
        stsVariableData.endPressureValue = '-';
        stsVariableData.endAvgTemp = '-';
        stsVariableData.endTemps = '-';
        stsConstData.capacity = '72';
        stsConstData.airPressure = '0.0825';

        //时序数据查询条件定义和赋值
        let queryConditionDto: HydrogenLeakageStatisticsConditionDto = {
          begintm: values.begintm,
          endtm: values.endtm,
          dynamo: values.dynamo,
        };
        statisticsHydrogenLeakage(queryConditionDto).then((res) => {
          console.log('statisticsHydrogenLeakage res ', res);
          if (res != null) {
            stsResultData.dynamoName = res.dynamoName;
            if (res.success === true) {
              //返回结果中标识为统计成功
              stsResultData.success = '统计成功。';
              stsResultData.msg = res.msg;
              stsResultData.tmrange = res.begintm + ' - ' + res.endtm;
              stsResultData.rate = numberFormatToString(res.rate, 2); //保留2位小数
              stsResultData.hours = numberFormatToString(res.hours, 2); //保留2位小数
              stsVariableData.beginPressureTime = res.beginPressureTime;
              stsVariableData.beginPressureValue = numberFormatToString(res.beginPressureValue, 5); //保留5位小数
              stsVariableData.beginAvgTemp = numberFormatToString(res.beginAvgTemp, 2); //保留2位小数
              //多个温度点号的数值
              if (!res.beginTemps) {
                //为空时不显示
              } else {
                console.log('res.beginTemps', res.beginTemps);
                let tempStr = '';
                Object.entries(res.beginTemps).forEach(([key, value]) => {
                  // console.log(key, value);
                  tempStr += numberFormatToString(value, 2) + ','; //保留2位小数
                });
                stsVariableData.beginTemps = tempStr;
              }
              stsVariableData.endPressureTime = res.endPressureTime;
              stsVariableData.endPressureValue = numberFormatToString(res.endPressureValue, 5); //保留5位小数
              stsVariableData.endAvgTemp = numberFormatToString(res.endAvgTemp, 2); //保留2位小数
              //多个温度点号的数值
              if (!res.endTemps) {
                //为空时不显示
              } else {
                console.log('res.endTemps', res.endTemps);
                let tempStr = '';
                Object.entries(res.endTemps).forEach(([key, value]) => {
                  // console.log(key, value);
                  tempStr += numberFormatToString(value, 2) + ','; //保留2位小数
                });
                stsVariableData.endTemps = tempStr;
              }
              stsConstData.capacity = numberFormatToString(res.capacity, 2); //保留2位小数
              stsConstData.airPressure = numberFormatToString(res.airPressure, 5); //保留5位小数
            } else {
              //返回结果中标识为统计失败，只显示错误提示信息
              stsResultData.msg = res.msg;
            }
          }
        });
      }

      onBeforeMount(() => {});
      onMounted(() => {});
      onUnmounted(() => {});

      return {
        registerForm,
        handleSubmit,
        stsResultData,
        stsResultSchema,
        stsVariableData,
        stsVariableSchema,
        stsConstData,
        stsConstSchema,
      };
    },
  });
</script>
